package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.PackageDao;
import com.itheima.pojo.Package;
import com.itheima.service.ReportService;
import com.itheima.util.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService{

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private PackageDao packageDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public List<Integer> countByMonth(List<String> list) {

        List<Integer> integerList = new ArrayList<>();

        for (String month : list) {
            String replacemonth = month.replace(".", "-");
            Integer memberCountBeforeDate = memberDao.findMemberCountBeforeDate(replacemonth);
            integerList.add(memberCountBeforeDate);
        }
        return integerList;
    }

    @Override
    public List<String> setmealNames() {
        return packageDao.findAllPackageName();
    }

    @Override
    public List<Map<String,Object>> setmealCount() {
        return packageDao.finAllPackageCount();
    }

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {

        Map<String,Object> map = new HashMap<>();

        //今日日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        System.out.println("今日日期:"+today);
        map.put("reportDate",today);

        //todayNewMember  今天的新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        map.put("todayNewMember",todayNewMember);

        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        map.put("totalMember",totalMember);

        //本周一
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //thisWeekNewMember  本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        map.put("thisWeekNewMember",thisWeekNewMember);

        //本月一号
        String monthofFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //本月新增会员数  thisMonthNewMember
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(monthofFirstDay);
        map.put("thisMonthNewMember",thisMonthNewMember);

        //todayOrderNumber  今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        map.put("todayOrderNumber",todayOrderNumber);


        //今日到诊数 todayVisitsNumber
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        map.put("todayVisitsNumber",todayVisitsNumber);

        //thisWeekOrderNumber  本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

        //thisMonthOrderNumber 本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(monthofFirstDay);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);

        //本月到诊数 thisMonthVisitsNumber
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(monthofFirstDay);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);


        List<Map> hotSetmeal = orderDao.findHotPackage();
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }

    @Override
    public Map<String, Object> exportBusinessReport() throws Exception {
        return getBusinessReportData();
    }

    //会员数量占比(性别)
    @Override
    public List<Map<String, Object>> getMemberSexCount() {
        return packageDao.findMemberSexCount();
    }

    //会员数量占比(年龄)
    @Override
    public List<Map<String, Object>> getMemberAgeCount() {
        List<Date> list = packageDao.findMemberAgeCount();
        List<Map<String,Object>> mapList = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        int count18 =0;
        int count30 =0;
        int count45 =0;
        int countelse =0;
        for (Date date : list) {
            if(date == null){
                continue;
            }
            int age = getAgeByBirthday(date);
            if(0<=age && age<=18 ){
                count18++;
            }else if(age>18 && age<=30){
                count30++;
            }else if(age>30&&age<=45){
                count45++;
            }else if(age>45){
                countelse++;
            }
        }
        map1.put("name","0-18");
        map1.put("value",count18);
        if((int)map1.get("value")>0){
            mapList.add(map1);
        }
        map2.put("name","18-30");
        map2.put("value",count30);
        if((int)map2.get("value")>0){
            mapList.add(map2);
        }
        map3.put("name","30-45");
        map3.put("value",count45);
        if((int)map3.get("value")>0){
            mapList.add(map3);
        }
        map4.put("name","45以上");
        map4.put("value",countelse);
        if((int)map4.get("value")>0){
            mapList.add(map4);
        }
        return mapList;
    }

    @Override
    public List<Integer> countByDate(List<String> result) {
        List<Integer> list = new ArrayList<>();
        for (String m : result) {
            m+="-32";
            Integer count=memberDao.findMemberCountByMonth(m);
            list.add(count);
        }
        return list;
    }

    //根据生日计算年龄
    public static int getAgeByBirthday(Date birthday) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthday)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        }
        return age;
    }



}
